package com.titanrise.crm.common.utils;

import java.util.function.*;

public class MaybePipeUtil {
    public static <A> Function<Maybe<A>, Maybe<A>> filter(Predicate<A> predicate) {
        return (maybe) -> maybe.filter(predicate);
    }

    public static <A, B> Function<Maybe<A>, Maybe<B>> map(Function<A, B> mapper) {
        return (maybe) -> maybe.map(mapper);
    }

    public static <A, B> Function<Maybe<A>, Maybe<B>> flatMap(Function<A, Maybe<B>> mapper) {
        return (maybe) -> maybe.flatMap(mapper);
    }

    public static <A, B> Function<Maybe<A>, Maybe<B>> andThen(Supplier<Maybe<B>> supplier) {
        return (maybe) -> maybe.flatMap((ignored) -> supplier.get());
    }

    public static <A> Function<Maybe<A>, Maybe<A>> forEach(Consumer<A> consumer) {
        return (maybe) -> maybe.forEach(consumer);
    }

    public static <A> Function<Maybe<A>, A> orElse(A other) {
        return (maybe) -> maybe.orElse(other);
    }

    public static <A, B> Function<Maybe<A>, B> match(Function<A, B> onJust, Supplier<B> onNothing) {
        return (maybe) -> maybe.match(onJust, onNothing);
    }

    public static <A> Function<Maybe<A>, A> orElseGet(Supplier<A> supplier) {
        return (maybe) -> maybe.orElseGet(supplier);
    }
}
